﻿using MesBSW.BLL.Mes;
using MesBSW.DAL.MesDB;
using MesBSW.DAL.MesDTO;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesBSW.Tools
{
    public static class LanguageTools
    {
        public static string InitCtrlTxetForLanguage(string langCode, string formName, string ctrlName, List<LanguageMulitDTO> langList)
        {
            string resStr = "";
            if (langList != null && langList.Count > 0)
            {
                var query = langList.Where(q => q.LanguageInfos.lang_code == langCode && q.LanguageItems.form_name == formName && q.LanguageItems
                .ctrl_name == ctrlName);
                resStr = query != null && query.Count() > 0 ? query.First().lang_text : "";
            }
            return resStr;
        }


        public static void ChangeControlTextForLangType(Form form, LanguageInfo langType)
        {
            if (form != null)
            {
                LanguageControler langCtrl = new LanguageControler();
                List<LanguageMulitDTO> mulitList = langCtrl.SearchLanguageMulitForType(langType);

                string typeName = form.GetType().FullName;
                if (mulitList != null && mulitList.Count > 0)
                {
                    foreach (System.Windows.Forms.Control tmpCtrl in form.Controls)
                    {
                        var query = mulitList.Where(q => q.LanguageItems.form_name == typeName && q.LanguageItems.ctrl_text == tmpCtrl.Name);
                        if (query != null && query.Count() > 0)
                        {
                            tmpCtrl.Text = query.First().lang_text;
                        }
                        ChangeControlTextForControl(typeName, tmpCtrl, mulitList);
                    }
                }
            }
        }

        /// <summary>
        /// 根据语言种类递归翻译界面中的各控件
        /// </summary>
        /// <param name="formType"></param>
        /// <param name="parentCtrl"></param>
        /// <param name="mulitList"></param>
        public static void ChangeControlTextForControl(string formType, System.Windows.Forms.Control parentCtrl, List<LanguageMulitDTO> mulitList)
        {
            if (mulitList != null && mulitList.Count > 0)
            {
                //处理DataGridView控件的栏位翻译
                if (parentCtrl.GetType().FullName.Contains("DataGridView"))
                {
                    foreach (DataGridViewColumn tmpClu in (parentCtrl as DataGridView).Columns)
                    {
                        var query = mulitList.Where(q => q.LanguageItems.form_name == formType && q.LanguageItems.ctrl_name == tmpClu.Name);
                        if (query != null && query.Count() > 0 && string.IsNullOrEmpty(query.First<LanguageMulitDTO>().lang_text) == false)
                        {
                            tmpClu.HeaderText = query.First<LanguageMulitDTO>().lang_text;
                        }
                    }
                }

                foreach (Control tmpCtrl in parentCtrl.Controls)
                {
                    Console.WriteLine(tmpCtrl.Name);                                                                                                                                                                                                                                 var query = mulitList.Where(q => q.LanguageItems.form_name == formType && q.LanguageItems.ctrl_name == tmpCtrl.Name);
                    if (query != null && query.Count() > 0 && string.IsNullOrEmpty(query.First<LanguageMulitDTO>().lang_text) == false)
                    {
                        tmpCtrl.Text = query.First<LanguageMulitDTO>().lang_text;
                    }

                    ChangeControlTextForControl(formType, tmpCtrl, mulitList);
                }
            }
        }
    }
}
